local jiaotun = fk.CreateSkill {
  name = "ls__jiaotun",
}

Fk:loadTranslationTable{
  ["ls__jiaotun"] = "狡吞",
  [":ls__jiaotun"] = "阶段技，你可重铸一张牌并选择一名角色，若其不为此牌的合法目标，你对其造成1点伤害，否则，你对此牌的所有不合法目标均造成1点伤害；以此法对自己造成伤害后，你获得场上的一张牌和1点护甲，然后此技能改为此阶段限两次。",

  ["#ls__jiaotun"] = "狡吞：你可以重铸一张牌并选择一名角色",
  ["@ls__jiaotun"] = "狡吞",
  ["#ls__jiaotun-choice"] = "狡吞：选择一名角色，获得其场上的牌",

  ["$ls__jiaotun1"] = "平贼之功，非我莫属。",
  ["$ls__jiaotun2"] = "贼乱数郡，宜速讨灭！",
}

local function Getenabletargets(card, player, target)
  if card:getFixedTargets(player) and #card:getFixedTargets(player) > 0 then
    return card:getFixedTargets(player)[1] == target
  else
    return card.skill:modTargetFilter(player, target, {}, card, {
      bypass_distances = false,
      bypass_times = true,
    })
  end
end

jiaotun:addEffect("active", {
  anim_type = "control",
  prompt = "#ls__jiaotun",
  card_num = 1,
  target_num = 1,
  --max_phase_use_time = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(jiaotun.name, Player.HistoryPhase) <= player:getMark("ls__jiaotun-phase")
  end, 
  card_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:recastCard(effect.cards, player, jiaotun.name)
    local card = Fk:getCardById(effect.cards[1])
    local isself = false
    --card:getAvailableTargets(player)
    if Getenabletargets(card, player, target) then
      local targets = table.filter(room:getAlivePlayers(true), function (p)
        return not Getenabletargets(card, player, p)
      end)
      if #targets > 0 then
        for _, p in ipairs(targets) do
          if not p.dead then
            room:damage({
              from = player,
              to = p,
              damage = 1,
              damageType = fk.NormalDamage,
              skillName = jiaotun.name,
            })
          end
        end
      end
      if table.contains(targets, player) then isself = true end
    else
      room:damage({
        from = player,
        to = target,
        damage = 1,
        damageType = fk.NormalDamage,
        skillName = jiaotun.name,
      })
      if player == target then isself = true end
    end
    if isself then
      player:addToPile("shield", room:getNCards(1), false, jiaotun.name)
      room:addPlayerMark(player, "ls__jiaotun-phase")
      local players = table.filter(room:getAlivePlayers(true), function (p) return #p:getCardIds("ej") > 0 end)
      if #players > 0 then
        local to = player.room:askToChoosePlayers(player, {
          min_num = 1,
          max_num = 1,
          targets = players,
          skill_name = jiaotun.name,
          prompt = "#ls__jiaotun-choice",
          cancelable = false,
        })
        if #to > 0 then
          local cid = room:askToChooseCard(player, {
            target = to[1],
            flag = "ej",
            skill_name = jiaotun.name,
          })
          room:obtainCard(player, cid, true, fk.ReasonPrey, player, jiaotun.name)
        end
      end
    end
  end,
})

return jiaotun